package edu.purdue.cs.aggr.accumulators;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.LinkedList;
import java.util.List;

import edu.purdue.cs.aggr.AbstractAccumulator;
import edu.purdue.cs.aggr.Accumulator;

public class YAccumulator extends AbstractAccumulator implements Accumulator<String, LinkedList<String>> {

	LinkedList<String> data = new LinkedList<String>();
	float Y = 2f;
	
	public YAccumulator(String sourceId)
  {
	  super(sourceId);
  }

  @Override
	public void writeExternal(ObjectOutput out) throws IOException {
    super.writeExternal(out);
		out.writeObject(data);
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
	  super.readExternal(in);
		data = (LinkedList<String>) in.readObject();
	}

	@Override
	public void initialize(String input) {
		
	}

	@Override
	public void addValue(String input) {
		data.add(input);
	}

	@Override
	public LinkedList<String> getAccumulation() {
		return data;
	}

	@Override
	public void addAccumulators(List<Accumulator<String, LinkedList<String>>> accumulators) {
		Accumulator<String, LinkedList<String>> firstAcc = accumulators.get(0);
		if (Y > 1) {
			System.out.println(" Y > 1");
			for (int i=0;i<Y; i++) {
				data.addAll(firstAcc.getAccumulation());
			}
		} else if (Y < 1) {
			System.out.println(" Y < 1");
			int outputRecods = (int) (data.size()*Y);
			LinkedList<String> newData = new LinkedList<String>();
			for (int i=0; i<outputRecods; i++) {
				newData.add(data.get(i));
			}
			data = newData;
		} else {
			System.out.println(" Y == 1");
			data.addAll(firstAcc.getAccumulation());
		}
	}
}
